﻿namespace FastColoredTextBoxNS
{
    using System;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;

    public partial class FindForm : Form
    {
        private bool firstSearch = true;

        private Place startPlace;

        private readonly FastColoredTextBox tb;

        public FindForm(FastColoredTextBox tb)
        {
            this.InitializeComponent();
            this.tb = tb;
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btFindNext_Click(object sender, EventArgs e)
        {
            this.FindNext(this.tbFind.Text);
        }

        public virtual void FindNext(string pattern)
        {
            try
            {
                RegexOptions opt = this.cbMatchCase.Checked ? RegexOptions.None : RegexOptions.IgnoreCase;
                if (!this.cbRegex.Checked)
                {
                    pattern = Regex.Escape(pattern);
                }
                if (this.cbWholeWord.Checked)
                {
                    pattern = "\\b" + pattern + "\\b";
                }
                //
                Range range = this.tb.Selection.Clone();
                range.Normalize();
                //
                if (this.firstSearch)
                {
                    this.startPlace = range.Start;
                    this.firstSearch = false;
                }
                //
                range.Start = range.End;
                if (range.Start >= this.startPlace)
                {
                    range.End = new Place(this.tb.GetLineLength(this.tb.LinesCount - 1), this.tb.LinesCount - 1);
                }
                else
                {
                    range.End = this.startPlace;
                }
                //
                foreach (var r in range.GetRangesByLines(pattern, opt))
                {
                    this.tb.Selection = r;
                    this.tb.DoSelectionVisible();
                    this.tb.Invalidate();
                    return;
                }
                //
                if (range.Start >= this.startPlace && this.startPlace > Place.Empty)
                {
                    this.tb.Selection.Start = new Place(0, 0);
                    this.FindNext(pattern);
                    return;
                }
                MessageBox.Show("Not found");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbFind_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.btFindNext.PerformClick();
                e.Handled = true;
                return;
            }
            if (e.KeyChar == '\x1b')
            {
                this.Hide();
                e.Handled = true;
                return;
            }
        }

        private void FindForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
            this.tb.Focus();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnActivated(EventArgs e)
        {
            this.tbFind.Focus();
            this.ResetSerach();
        }

        private void ResetSerach()
        {
            this.firstSearch = true;
        }

        private void cbMatchCase_CheckedChanged(object sender, EventArgs e)
        {
            this.ResetSerach();
        }
    }
}